package com.ydc.ln.tutorial.gui.app.minieditor;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JTextArea;

/**
 * 实现TextArea的功能，并使文字可以打印输出
 * 
 * @author ydc
 * @date 上午12:59:58 2013-4-13
 */
public class PrintableTextArea extends JTextArea implements Printable {

    /**
     * @Fields serialVersionUID
     * 
     */
    private static final long serialVersionUID = 3571801255972109293L;

    // 属性
    String str;
    Font font;
    PrinterJob pj;
    PageFormat defaultFormat;
    int visiblePageWidth = 0, visiblePageHeight = 0;
    final static int PageX = 10, PageY = 10;

    // 构造方法
    PrintableTextArea(int rows, int columns) {
        super(rows, columns);
    }

    // 打印方法
    public void printIt(String str, Font font) {
        this.str = str;
        this.font = font;
        pj = PrinterJob.getPrinterJob();
        defaultFormat = pj.defaultPage();
        pj.setPrintable(this, defaultFormat);
        visiblePageWidth = (int) defaultFormat.getImageableWidth();
        visiblePageHeight = (int) defaultFormat.getImageableHeight();
        if (pj.printDialog()) {
            try {
                pj.print();
            } catch (PrinterException e) {

            }
        }
    }

    // print方法
    public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException {
        if (page >= 1) {
            return Printable.NO_SUCH_PAGE;
        }
        Graphics2D g2 = (Graphics2D) g;
        g2.translate((int) pf.getImageableX(), (int) pf.getImageableY());
        g2.setFont(font);
        int fontHeight = 0, fontStringWidth = 0;
        int line = 0;
        String s = null;
        FontMetrics fm = getFontMetrics(font);
        fontHeight = fm.getHeight();
        try {
            DataOutputStream dos =
                    new DataOutputStream(new FileOutputStream("$temp.$$p"));
            dos.writeBytes(str);
            dos.close();
        } catch (IOException e) {

        }
        try {
            BufferedReader br = new BufferedReader(new FileReader("#temp.$$p"));
            s = br.readLine();
            while (s != null) {
                if (s.length() == 0) {
                    g2.drawString("", PageX, PageY + fontHeight * (line++));
                } else {
                    // g2.drawString(s, PageX, PageY + fontHeight * (line++));
                    fontStringWidth = fm.stringWidth(s);
                    if (fontStringWidth > visiblePageWidth) {
                        String s1, s2;
                        int goBack = 0;
                        while (fm.stringWidth(s1 =
                                s.substring(0, s.length() - goBack)) > visiblePageWidth) {
                            goBack++;
                        }
                        s1 = s.substring(0, s.length() - goBack);
                        s2 = s.substring(s.length() - goBack - 1);
                        g2.drawString(s1, PageX, PageY + fontHeight * (line++));
                        g2.drawString(s2, PageX, PageY + fontHeight * (line++));
                    } else {
                        g2.drawString(s, PageX, PageY + fontHeight * (line++));
                    }
                }
                s = br.readLine();
            }
            br.close();
        } catch (IOException e) {

        }

        return Printable.PAGE_EXISTS;
    }
}
